package admin

import (
	"github.com/gin-gonic/gin"
	"zhuandan/data_source"
	"zhuandan/models"
	"zhuandan/utils"
)

func Orderywdtongbu(ctx *gin.Context) {

	var ordertongbu []models.Orderywdtongbu
	data_source.Db.Where("chuli = 1").Find(&ordertongbu)

	var classifys []models.Classify
	data_source.Db.Find(&classifys)

	for _, o := range ordertongbu {
		var user models.User
		data_source.Db.Where("id = ?", o.Uid).First(&user)
		var weixin models.YwWeixin
		data_source.OtherDb.Select("id,unionid").Where("unionid = ?", user.Unionid).First(&weixin)
		var infobuys []models.YwWeixinInfoBuy
		data_source.OtherDb.Where("account_id = ?", weixin.ID).Find(&infobuys)

		var ywdid []int64
		for _, i := range infobuys {
			cunzai := 1
			for _, y := range ywdid {
				if i.InfoId == y {
					cunzai = 2
				}
			}
			if cunzai == 1 {
				ywdid = append(ywdid, i.InfoId)
			}
		}

		var infos []models.YwWeixinInfo
		data_source.OtherDb.Where("is_other = 0").Where("id in (?)", ywdid).Order("id dssc").Find(&infos)

		for _, v := range infos {
			var procure models.Procure
			data_source.Db.Where("ywdid = ?", v.ID).First(&procure)
			if procure.ID == 0 {
				var classify models.Classify
				for _, c := range classifys {
					if utils.Int64string(v.ItemType) == c.Value {
						classify = c
					}
				}

				var buys []models.YwWeixinInfoBuy
				data_source.OtherDb.Where("info_id = ?", v.ID).Find(&buys)

				var num int64
				for _, b := range buys {
					if b.RefundMemo == "" {
						num = num + int64(b.Num)
					}
				}

				pro := models.Procure{
					Uid:           2,
					Province:      v.AreaProvince,
					Provinceid:    v.AreaProvinceCode,
					City:          v.AreaCity,
					Cityid:        v.AreaCityCode,
					County:        v.AreaDistrict,
					Countyid:      v.AreaDistrictCode,
					Title:         v.Title,
					Content:       v.Content,
					Num:           utils.StrToInt64(v.Num),
					Price:         v.Price,
					Name:          v.ContactName,
					Phone:         v.ContactTel,
					Classifyid:    classify.ID,
					Classifyvalue: classify.Value,
					Classifytitle: classify.Title,
					Addtime:       v.Ctime,
					State:         1,
					Fasong:        2,
					Buynum:        num,
					Ywdid:         v.ID,
				}
				data_source.Db.Create(&pro)

				var procureorders []models.Procureorder
				for _, b := range buys {
					var status int64
					var tkliyou string
					var tktime int64
					if b.RefundMemo == "" {
						status = 1
					} else {
						status = 2
						tkliyou = b.RefundMemo
						tktime = b.UpdateTime
					}

					procureorder := models.Procureorder{
						Uid:       user.ID,
						Addtime:   b.Ctime,
						Procureid: pro.ID,
						Num:       int64(b.Num),
						Phone:     user.Phone,
						Price:     b.Money,
						Status:    status,
						Title:     v.Title,
						Fbuid:     2,
						Daozhang:  2,
						Tkliyou:   tkliyou,
						Tktime:    tktime,
						State:     int64(b.Progress),
					}
					procureorders = append(procureorders, procureorder)
				}
				data_source.Db.Create(&procureorders)

			}

		}

	}

}
